package com.greenline.search.test.test.queryanalysis;

import com.greenline.search.test.dao.BaseObject;
import com.greenline.search.test.dao.DepartmentDao;
import com.greenline.search.test.utils.ArrayUtils;
import com.greenline.search.test.utils.FileUtils;
import com.greenline.search.test.utils.JSONStreamParserForAnalyze;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: liulei
 * Date: 13-12-16
 * Time: 下午3:47
 * To change this template use File | Settings | File Templates.
 */
public class UserQueryDoctorSearchDepartmentAnalysis {

    private static Logger logger = LoggerFactory.getLogger(UserQueryDoctorSearchDepartmentAnalysis.class);

    public static void main(String[] args) {
        //keyword list
        String classPath = FileUtils.class.getResource("/").getPath();
        String filePath = classPath+"dic/user_keyword.txt";
        List<String> keywordList = FileUtils.readFileAsString(filePath, "GBK");

        //get hospital department and standard department map
        List<BaseObject> departmentList = new DepartmentDao().getHospDeptAndDepartment();
        Map<String,String> hospDeptAndDepartmentMap = new HashMap<String, String>();
        for(BaseObject object : departmentList){
            hospDeptAndDepartmentMap.put(object.getUuid(),object.getName());
        }

        long startTime = System.currentTimeMillis();
        //request url
        String baseUrl = "http://10.10.100.153:9999/doctor_search?fl=department_uuid&sort=doctor_haoyuan_level%20desc,score%20desc,pop%20desc&rows=10&q=";
        System.out.println("test start...");

        //storage the log info
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < keywordList.size(); i++){
            if(i % 1000 == 0){
                System.out.println("Deal with " + i + " keywords already.");
            }
            List<String> deptUUIDList = null;
            try {
                deptUUIDList = JSONStreamParserForAnalyze.parseDoctorSearchResponse(baseUrl+URLEncoder.encode(keywordList.get(i),"UTF-8"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            String deptUUID = ArrayUtils.getTheMaxSumObject(deptUUIDList);
            sb.append(keywordList.get(i)).append("\t")
                    .append(hospDeptAndDepartmentMap.get(deptUUID)).append("\n");
        }
        logger.info(sb.toString());
        System.out.println("test end...");
        //total cost time
        long costTime = System.currentTimeMillis() - startTime;
        System.out.println("The totalTime is " + (costTime/1000) + " seconds.");
    }

}
